/*
 * Filename:    OrgReviewController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvaluationMarkContentDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.IntegrityEvalDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.biz.service.OrgApproveService;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.constants.OrgReviewConstants;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dto.MessageInfoDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dto.OrgEvaluationOpinionDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dto.OrgFinishConfirmDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dto.OrgReviewComfirmDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 该控制器主要是对定点机构诚信考评审批
 * 
 * @author EX-LUORONG001
 * 
 */
@Controller
@RequestMapping("/orgApprove/")
public class OrgApproveController {

	@BizServicesProxy(beanName = "orgApproveServiceImpl")
	private OrgApproveService orgApproveService;

	/**
	 * 根据id加载对应的诚信单位考评详情
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadIntgrityEvalInfo.do")
	@ResponseBody
	public Map<String, Object> loadIntgrityEvalInfo(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String evaluationCode = request.getParameter("evaluationCode");
		IntegrityEvalDTO bean = null;
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			bean = this.orgApproveService.getIntegrityEvalDTOByTypeAndID(evaluationCode, user);
			result.put("flag", true);
			result.put("data", bean);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;

	}

	@RequestMapping("loadIntgrityEvalTypeInfo.do")
	@ResponseBody
	public Map<String, Object> loadIntgrityEvalTypeInfo(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String evaluationCode = request.getParameter("evaluationCode");
		List<OrgReviewComfirmDTO> list = null;
		if (StringUtils.hasText(evaluationCode)) {
			list = this.orgApproveService.processLoadEvalInfo(evaluationCode, user);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("flag", true);
		result.put("list", list);
		return result;

	}

	@RequestMapping("loadEvaluationFinishInfo.do")
	@ResponseBody
	public Map<String, Object> loadEvaluationFinishInfo(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String evaluationCode = request.getParameter("evaluationCode");
		List<OrgFinishConfirmDTO> list = null;
		if (StringUtils.hasText(evaluationCode)) {
			list = this.orgApproveService.loadEvaluationFinishInfo(evaluationCode, user);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("flag", true);
		result.put("list", list);
		return result;

	}

	/**
	 * 根据id加载对应的诚信考评详情
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadIntgrityEvalDetailInfo.do")
	@ResponseBody
	public Map<String, Object> loadIntgrityEvalDetailInfo(HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		List<EvaluationMarkContentDTO> list = null;
		String evaluRuleId = request.getParameter("evaluRuleId");
		String evalChildId = request.getParameter("intgrityEvalChildId");
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		try {
			if (StringUtils.hasText(evaluRuleId)) {
				list = this.orgApproveService.loadMarkContentDTOByRuleId(evaluRuleId, evalChildId, user);
				result.put("flag", OrgReviewConstants.FLAG_TURE);
				result.put("result", list);
			}

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据诚信考评表子表 主键去加载对应的意见信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getEvaluationOpinionById.do")
	@ResponseBody
	public Map<String, Object> getEvaluationOpinionById(HttpServletRequest request) {

		Map<String, Object> result = new HashMap<String, Object>();

		String evalChildId = request.getParameter("evalChildId");

		OrgEvaluationOpinionDTO bean = new OrgEvaluationOpinionDTO();
		bean.setIntgritEvalChildId(evalChildId);
		bean.setTaskStatus(OrgReviewConstants.eval_status_3);
		// 审核有效的审核对象
		OrgEvaluationOpinionDTO auditDataIsValid = this.orgApproveService.getEvaluatonOpinionDTO(bean);
		bean.setIsValid(OrgReviewConstants.is_valid_1);

		OrgEvaluationOpinionDTO auditDataIsNotValid = this.orgApproveService.getEvaluationOpinionInfoByIsValid(bean);

		//if (null != auditDataIsNotValid) {
		//	result.put("auditData", auditDataIsNotValid);
		//} else 
		if (null != auditDataIsValid) {
			result.put("auditData", auditDataIsValid);
		} else {
			result.put("auditData", "");
		}

		OrgEvaluationOpinionDTO approveParamater = new OrgEvaluationOpinionDTO();
		approveParamater.setIntgritEvalChildId(evalChildId);
		approveParamater.setTaskStatus(OrgReviewConstants.eval_status_5);

		// 有效的
		OrgEvaluationOpinionDTO approveDataIsValid = this.orgApproveService.getEvaluatonOpinionDTO(approveParamater);

		approveParamater.setIsValid(OrgReviewConstants.is_valid_1);
		OrgEvaluationOpinionDTO approveDataIsNotValid = this.orgApproveService
				.getEvaluationOpinionInfoByIsValid(approveParamater);

		result.put("flag", true);

		if (approveDataIsValid != null) {
			result.put("approveData", approveDataIsValid);
		} else if (approveDataIsNotValid != null) {
			result.put("approveData", approveDataIsNotValid);
		} else {
			result.put("approveData", "");
		}

		return result;
	}

	/**
	 * 审批诚信考评信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("auditEvaluationInfo.do")
	@ResponseBody
	public MessageInfoDTO auditEvaluationInfo(HttpServletRequest request) {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");

		String evalChildDataId = request.getParameter("evalChildDataId");
		String auditOpinion = request.getParameter("auditOpinion");
		String auditDesc = request.getParameter("auditDesc");
		String evaluationCode = request.getParameter("evaluationCode");

		MessageInfoDTO bean = new MessageInfoDTO();

		try {
			if (StringUtils.hasText(evalChildDataId) && StringUtils.hasText(auditOpinion)) {
				bean = this.orgApproveService.processEvaluationApproveInfo(evalChildDataId, auditOpinion, auditDesc,
						evaluationCode, user);
			}
		} catch (Exception es) {
			bean.setSuccess("false");
			bean.setMessage(es.getCause().getMessage());
			es.printStackTrace();
		}

		return bean;
	}

	/**
	 * 审批 批量提交
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "approveBatchSumit.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> approveBatchSumit(HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			String auditOpinion = transferJson.get("auditOpinion").toString();
			String auditDesc = transferJson.get("auditDesc").toString();
			String evaluationCodes = transferJson.get("evaluationCodes").toString();
			JSONArray ja = JSON.parseArray(evaluationCodes);
			List<String> list = new ArrayList<String>();
			for (int i = 0; i < ja.size(); i++) {
				String tempStr = ja.getString(i);
				list.add(tempStr);
			}
			if (!StringUtils.hasText(auditOpinion)) {
				result.put(OrgReviewConstants.MSG_KEY, "审核意见不能为空");
				result.put(OrgReviewConstants.STATE_KEY, OrgReviewConstants.FAILED_VALIDATE_STATE_VALUE);
				return result;
			}
			if (OrgReviewConstants.opinion_0.equals(auditOpinion) && !StringUtils.hasText(auditDesc)) {
				result.put(OrgReviewConstants.MSG_KEY, "意见描述不能为空");
				result.put(OrgReviewConstants.STATE_KEY, OrgReviewConstants.FAILED_VALIDATE_STATE_VALUE);
				return result;
			}
			if (null == list || list.size() <= 0) {
				result.put(OrgReviewConstants.MSG_KEY, "考评选项不能为空");
				result.put(OrgReviewConstants.STATE_KEY, OrgReviewConstants.FAILED_VALIDATE_STATE_VALUE);
				return result;
			}
			result = this.orgApproveService.approveBatchSumit(auditOpinion, auditDesc, list, user);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 机构诚信审批撤销
	 */
	@RequestMapping(value = "cancelMedialIntegrityApprove.do")
	@ResponseBody
	public Map<String, Object> cancelMedialIntegrityApprove(HttpServletRequest request) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		Map<String, Object> map = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		map.put("evaluationCode", request.getParameter("evaluationCode"));
		map.put("user", user);
		try {
			boolean flag = this.orgApproveService.cancelMedialIntegrityApprove(map);
			returnMap.put("state", flag);
			if (flag) {
				returnMap.put("msg", "操作成功!");
			} else{
				returnMap.put("msg", "该笔记录不能撤销!");
			}
		} catch (Exception e) {
			returnMap.put("state", false);
			returnMap.put("msg", e.getMessage());
			e.printStackTrace();
		}
		return returnMap;
	}
}
